<?php
namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Model\Product;

class ProductTransformer extends TransformerAbstract{

    private static $status = [
        'on_sale' => '在售',
        'sale_out' => '售罄'
    ];


    public function transform(Product $item){
        return [
            'id' => $item->id,
            'product_category_id' => $item->product_category_id,
            'bistro_id' => $item->bistro_id,
            'img' => $item->img,
            'status' => ProductTransformer::$status[$item->status],
            'name' => $item->name,
            'discount' => $item->discount,
            'price' => $item->price,
            'sale_count' => $item->sale_count,
            'zan_number' => $item->zan_number,
            'description' => $item->description,
            'category' => $item->category()->first(),
            'created_at' =>$item->created_at->toDateTimeString(),
            'updated_at' =>$item->updated_at->toDateTimeString()
        ];
    }

    public static function forBackendList() {
        return function (Product $item){
            return [
                'id' => $item->id,
                'bistro_id' => $item->bistro_id,
                'img' => $item->img,
                'status' => ProductTransformer::$status[$item->status],
                'name' => $item->name,
                'discount' => $item->discount,
                'price' => $item->price,
                'sale_count' => $item->sale_count,
                'zan_number' => $item->zan_number,
                'description' => $item->description,
                'created_at' =>$item->created_at->toDateTimeString(),
                'updated_at' =>$item->updated_at->toDateTimeString()
            ];
        };
    }

}